<template>
  <el-cascader :value="value" :options="depts" :props="props" @change="handleChange" />
</template>

<script>
import { departmentGetService } from '@/api/department'
import { transListToTreeData } from '@/utils'
export default {
  props: {
    value:
    {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      depts: [],
      props: {
        label: 'name',
        value: 'id'
      }
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      this.depts = transListToTreeData(await departmentGetService(), 0)
      // console.log(this.depts)
    },
    handleChange(value) {
      // console.log(value) // value[value.list.length-1]
      // this.$emit('input', value)
      if (value.length) {
        this.$emit('input', value[value.length - 1])
      } else {
        this.$emit('input', null)
      }
    }
  }
}
</script>
